package edu.tum.in.campar.twodui.studentcanteen;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import edu.tum.in.campar.twodui.studentcanteen.model.DataProvider;

public class StartupActivity extends ListActivity {
	/** Called when the activity is first created. */
	private static final int SPINNER_DIALOG = 0x1;
	private AsyncTask<Void, Void, Void> loadMenuAsyncTask;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if ((networkInfo != null) && (networkInfo.isConnected())){
        
	        loadMenuAsyncTask = new AsyncTask<Void, Void, Void>()
	        {
	        	@Override
	        	protected void onPreExecute() {
	        		super.onPreExecute();
	        		showDialog(SPINNER_DIALOG);
	        	}
	        	
	        	@Override
	        	protected Void doInBackground(Void... params) {
	        		DataProvider.get().update(StartupActivity.this);
	        		return null;
	        	}
	        	
	        	protected void onPostExecute(Void result) {
	        		removeDialog(SPINNER_DIALOG);
	        		loadMenuList();
	        		
	        		loadMenuAsyncTask = null;
	        	};
	        	
	        	protected void onCancelled() {
	        		onPostExecute(null);
	        	}
	        }.execute();
		}
		else
			loadMenuList();

    }
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		Intent i;
		
		// hardcoded positions for now
		switch(position)
		{
		case 0:
			i = new Intent(this, SLDateOverviewActivity.class);
			startActivity(i);
			break;
		case 1:
			i = new Intent(this, TVDateAndMealActivity.class);
			startActivity(i);
			break;
		case 2:
			i = new Intent(this, DVDateAndMealActivity.class);
			startActivity(i);
			break;
		}
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch(id)
		{
		case SPINNER_DIALOG:
    		ProgressDialog spinner = new ProgressDialog(StartupActivity.this);
    		spinner.setCancelable(false);
    		spinner.setTitle(R.string.spinner_downloading);
    		spinner.setCancelable(true);
    		spinner.setOnCancelListener(new OnCancelListener() {
				
				@Override
				public void onCancel(DialogInterface dialog) {
					loadMenuAsyncTask.cancel(true);					
				}
			});
    		
    		return spinner;
		default:
			return super.onCreateDialog(id);
		}
	}
	
	private void loadMenuList(){
		DataProvider.get().load(StartupActivity.this);
		ListView list = getListView();
		list.setAdapter(new ArrayAdapter<String>(StartupActivity.this, android.R.layout.simple_list_item_1, new String[]{getString(R.string.view_select_simplelist), getString(R.string.view_select_treeview), getString(R.string.view_select_dateview)}));
	}
}